<?php

if (!function_exists('response_with_token')) {
    function response_with_token($accessToken)
    {
        return response()->json([
                'access_token' => $accessToken,
                'token_type' => 'bearer',
                'expires_in' => auth()->factory()->getTTL() * 60
            ]
        );
    }
}

if (!function_exists('response_with_extra')) {
    function response_with_extra($message, array $extra = [], $statusCode = 200)
    {
        $response_data = array_merge([
            'message' => $message
        ], $extra);
        return response()->json($response_data, $statusCode);
    }
}

if (!function_exists('response_with_message')) {
    function response_with_message($message, $statusCode = 200)
    {
        return response_with_extra($message, [], $statusCode);
    }
}

if (!function_exists('response_success')) {
    function response_success(array $extra = [])
    {
        return response_with_extra('Success', $extra, 200);
    }
}

if (!function_exists('response_success_extra')) {
    function response_success_extra(array $extra = [], $statusCode = 200)
    {
        $response_data = array_merge([
            'message' => 'Success'
        ], $extra);
        return response()->json($response_data);
    }
}

if (!function_exists('response_with_code')) {
    function response_with_code($message, $code, $statusCode = 500)
    {
        return response()->json([
            'message' => $message,
            'code' => $code
        ], $statusCode);
    }
}

if (!function_exists('format_size')) {
    function format_size(int $size, string $miniUnit = 'Bytes')
    {
        if ($size == 0) {
            return '0 ' . $miniUnit;
        }
        $size_name = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        $log = intval(floor(log($size, 1024)));
        $offset = array_search($miniUnit, $size_name);
        $pow = $log > $offset ? $log : $offset;
        return round($size / pow(1024, $pow), 2) . ' ' . $size_name[$pow];
    }
}